package nave.model;

import nave.Constants;
import nave.Point;


public class Shot extends GameObject {

	/*
	 * To be set for the view
	 */
	public static int WIDTH = 8;
	public static int HEIGHT = 8;

	/*
	 * Attributes
	 */
	private Point deltaMove;
	private Point head;
	private final int direction;
	private Spaceship parent;
	private boolean hasLeftBounds;

	/*
	 * Constants
	 */
	public final static int SPEED = 4;
	
	/*
	 * Getters and Setters
	 */
	public Point getHead() {
		return head;
	}
	
	public Spaceship getParent() {
		return parent;
	}

	public boolean hasLeftBounds() {
		return hasLeftBounds;
	}
	
	public int getDirection() {
		return this.direction;
	}

	/*
	 * Constructor
	 */
	public Shot( int direction, Spaceship parent ) {
		
		super(nave.Constants.ID_BULLETS);
		
		this.parent = parent;
		this.hasLeftBounds = false;

		this.drawPoint = (Point) parent.getDrawPositon().clone();
		this.position = (Point) parent.getPosition().clone();
		deltaMove = new Point(0, 0);

		switch( direction )
		{
			case Spaceship.DIRECTION_UP:
			{
				direction = 1;
				deltaMove.y = -1 * SPEED;
				drawPoint.translate(Constants.TILE_WIDTH / 2 - WIDTH / 2, 0);
				head = new Point(drawPoint.x + Constants.TILE_WIDTH / 2,drawPoint.y);
				break;
			}
			case Spaceship.DIRECTION_DOWN:
			{
				direction = 1;
				deltaMove.y = 1 * SPEED;
				drawPoint.translate(Constants.TILE_WIDTH / 2 - WIDTH / 2, Constants.TILE_HEIGHT - HEIGHT);
				head = new Point(drawPoint.x + Constants.TILE_WIDTH / 2, drawPoint.y + HEIGHT);
				break;
			}
			case Spaceship.DIRECTION_LEFT:
			{
				direction = 0;
				deltaMove.x = -1 * SPEED;
				drawPoint.translate(0, Constants.TILE_HEIGHT / 2 - HEIGHT / 2);
				head = new Point(drawPoint.x, drawPoint.y + Constants.TILE_HEIGHT / 2);
				break;
			}
			case Spaceship.DIRECTION_RIGHT:
			{
				direction = 0;
				deltaMove.x = 1 * SPEED;
				drawPoint.translate(Constants.TILE_WIDTH - WIDTH, Constants.TILE_HEIGHT / 2 - HEIGHT / 2);
				head = new Point(drawPoint.x + WIDTH, drawPoint.y + Constants.TILE_HEIGHT / 2);
				break;
			}
			default:
				head = new Point();
				collided();
		}
		
		this.direction = direction;
	}

	/*
	 * Public methods
	 */
	public void placeInStage() {}

	public void collided() {
		parent.decreaseBulletsFired();
		hasLeftBounds = true;
	}
	
	public void update (float delta) {
		drawPoint.translate(deltaMove.x, deltaMove.y);
		head.translate(deltaMove.x, deltaMove.y);
		
		position.x = head.x / Constants.TILE_WIDTH;
		position.y = head.y / Constants.TILE_HEIGHT;
	}

	@Override
	public void placeOnStage() {}

}
